package com.hp.attendance.serlvet.empmanage.zaizhi;

import com.hp.attendance.entity.Department;
import com.hp.attendance.entity.History;
import com.hp.attendance.entity.Position;
import com.hp.attendance.service.DepartmentService;
import com.hp.attendance.service.HistoryService;
import com.hp.attendance.service.PositionService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@WebServlet("/historyZZPreAdd")
public class EmpManageZZHistoryPreAddServlet extends HttpServlet {
    private HistoryService historyService = new HistoryService();
    private DepartmentService departmentService = new DepartmentService();
    private PositionService positionService = new PositionService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ArrayList<Integer> list1 = new ArrayList<Integer>();
            List<History> list = historyService.getList();
            for (History history : list) {
                Integer employee_number = history.getEmployee_number();
                list1.add(employee_number);
            }
            Integer max = Collections.max(list1);
            int employee_number = max+1;
            List<Department> departments = departmentService.getList();
            List<Position> positions = positionService.getList();
            req.setAttribute("departments",departments);
            req.setAttribute("positions",positions);
            req.setAttribute("employee_number",employee_number);
            req.getRequestDispatcher("pages/employee_add.jsp").forward(req,resp);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
